import {AsyncStorage} from 'react-native'
import Request from "../utils/Request";

const STORAGE_KEY = "STORAGE_KEY_mark";
export default {
    namespace: 'mark',
    state: {
        list:[]
    },
    reducers: {
        save(state, {payload}) {
            return {...state, ...payload}
        }
    },
    effects: {
        //保存到本地
         * saveToStorage({payload}, {call}) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload))
        },

        //本地恢复
         * restoreFromStorage({payload}, {call, put, select}) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            yield put({
                type: 'save',
                payload: {...JSON.parse(data)}
            });
        },

        * fetchList({payload}, {call, put, select}) {
            const data = yield call(Request.get,Request.getApi().markList);
            if(data){
                yield put({
                    type:'save',
                    payload:{list:data.data.map(item=>({...item,label:item.name,value:item.id}))}
                })
            }
        },
    },
    subscriptions: {
        setup({dispatch}) {
            // dispatch({
            //     type: 'fetchList'
            // })
        }
    },
};
